<template>
  <div class="autumn-container">
    <!-- 面包 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/dashboard' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item :to="{ path: '/wms/warehouseLocation/warehouseLocationList' }">库位信息列表</el-breadcrumb-item>
      <el-breadcrumb-item>库位信息</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 卡片 -->
    <div class="autumn-center">
      <div class="edit-form">
        <el-form size="large" label-width="90px" disabled :model="editForm">

          <el-row>
            <el-col :span="12">
              <el-form-item label="所属仓库" prop="warehouseInfoId">
                <el-select v-model="editForm.warehouseInfoId">
                  <el-option v-for="(item, index) in warehouses" :key="index" :label="item.code + '-' + item.name"
                    :value="item.id">
                  </el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="所属库区" prop="warehouseAreaId">
                <el-select v-model="editForm.warehouseAreaId">
                  <el-option v-for="(item, index) in warehouseAreas" :key="index" :label="item.code + '-' + item.name"
                    :value="item.id">
                  </el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="库位编码" prop="code">
                <el-input v-model="editForm.code" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="库位名称" prop="name">
                <el-input v-model="editForm.name" />
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item label="备注" prop="remark">
                <el-input type="textarea" resize="none" v-model="editForm.remark" />
              </el-form-item>
            </el-col>
          </el-row>

        </el-form>

        <div class="form-foot-button">
          <el-button @click="cancel">返回</el-button>
        </div>
      </div>
    </div>
  </div>
</template>

<script lang="ts" setup>
import { reactive, ref, watch } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { viewWarehouseLocation } from 'src/api/wms/warehouseLocation'
import { listWarehouseInfo } from 'src/api/wms/warehouseInfo'
import { listWarehouseArea } from 'src/api/wms/warehouseArea'

//查询仓库/库区
const warehouses = ref<Array<any>>([])
listWarehouseInfo().then(({ data }) => {
  warehouses.value = data
})

const warehouseAreas = ref<Array<any>>([])
const initWarehouseArea = (warehouseInfoId: string) => {
  listWarehouseArea({ warehouseInfoId }).then(({ data }) => {
    warehouseAreas.value = data
  })
}

// 定义表单数据
const editForm = reactive({
  id: '',
  warehouseInfoId: '',
  warehouseAreaId: '',
  code: '',
  name: '',
  remark: ''
})

// 修改时填充表单数据
const route = useRoute()
if (route.query.id) {
  const id = route.query.id as string
  viewWarehouseLocation(id).then(({ data }) => {
    Object.assign(editForm, data)
  })
}

watch(() => editForm.warehouseInfoId, (newVal: string) => {
  if (!newVal) {
    return
  }
  initWarehouseArea(newVal)
})

// 返回
const router = useRouter()
const cancel = () => {
  router.push('/wms/warehouseLocation/warehouseLocationList')
}

</script>

<style lang="scss" scoped></style>

